import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:image_pickers/image_pickers.dart';
import 'package:thinking/entity/ImagePickerEntity.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/config/_.dart';
import 'Line.dart';
import 'dart:io';
class ImagePickerWidget extends StatelessWidget{
  final int imageListLength;
  final Widget child;
  final Function onChange;
  final List<String> modalBottomList=['拍照','相册'];

  ImagePickerWidget({required this.child,required this.onChange,required this.imageListLength});

  Future<void> showModalBottom(BuildContext context){

    return showModalBottomSheet(
        context: context,
        elevation: 10,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.vertical(top: Radius.circular(20))),
        builder: (BuildContext context) {
          return Container(
            height: Screen.setHeight(150),
            padding: EdgeInsets.only(top: Screen.setHeight(10)),
            child: Column(
                children:  modalBottomList.map((e) =>  Column(
                  children: [
                    Container(
                      height: 50,
                      child: TextButton(
                        style: ButtonStyle(
                          overlayColor: MaterialStateProperty.all(
                              ColorConfig.LineColor
                          ),
                        ),
                        child: Center(
                          child: Text(
                            e,
                            style: TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.w600,
                                color: Colors.black
                            ),
                          ),
                        ),
                        onPressed: (){
                         switch(e){
                           case('拍照'):
                              Modular.to.pop();
                              image('openCamera');
                              return;
                           case('相册'):
                              Modular.to.pop();
                              image('xxx');
                              return;
                           default:
                             return ;
                         }
                        },
                      ),
                    ),
                    Line(height: 1,width: Screen.setWidth(Screen.width(context)-40),),
                  ],
                )).toList()

            ),
          );
        });
  }

  Future<void> image(String type) async {

    switch(type){
      case 'openCamera':
        ImagePickers.openCamera().then((Media? media){
          return onChange([ImagePickerEntity(file: File(media!.path!),filePath: media.path,fileMedia: media)]);
        });
        break;
      default:
        var listImages =   await ImagePickers.pickerPaths(
            galleryMode: GalleryMode.image,
            selectCount: imageListLength,
            showGif: true,
            showCamera: true,
            compressSize: 500,
            uiConfig: UIConfig(uiThemeColor: ColorConfig.ThemeColor));
        return onChange(listImages.map((e) => ImagePickerEntity(file: File(e.path!),filePath: e.path!,fileMedia: e)).toList());
    }
    }


  @override
  Widget build(BuildContext context) {
      return GestureDetector(
        onTap: (){
          showModalBottom(context);
        },
        child: child,
      );
  }

}